package com.example.demo.util;

import com.example.demo.excel.util.ResultStatus;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * web请求的统一返回结果
 * 以map的形式
 *
 * @author SUN
 */
public class ResultUtil {

    public static final ResultStatus SUCCESS = ResultStatus.SUCCESS;
    public static final ResultStatus DEFAULT_ERROR = ResultStatus.ERROR;

    /**
     * code:
     * message:
     *
     * @return
     */
    public static Map<String, Object> success() {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", SUCCESS.getCode());
        result.put("message", SUCCESS.getMessage());
        return result;
    }

    /**
     * @param data
     * @return
     */
    public static Map<String, Object> success(Map<String, Object> data) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", SUCCESS.getCode());
        result.put("message", SUCCESS.getMessage());
        if (MapUtils.isEmpty(data)) {
            return success();
        }
        result.putAll(data);
        return result;
    }

    /**
     * @param key   not blank
     * @param value not null
     * @return
     */
    public static Map<String, Object> success(String key, Object value) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", SUCCESS.getCode());
        result.put("message", SUCCESS.getMessage());
        if (StringUtils.isBlank(key)) {
            throw new RuntimeException("参数错误");
        }
        result.put(key, value);
        return result;
    }

    /**
     * @return
     */
    public static Map<String, Object> error() {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", DEFAULT_ERROR.getCode());
        result.put("message", DEFAULT_ERROR.getMessage());
        return result;
    }

    /**
     * @return
     */
    public static Map<String, Object> error(String message) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", DEFAULT_ERROR.getCode());
        result.put("message", message);
        return result;
    }

    /**
     * @param code
     * @param message
     * @return
     */
    public static Map<String, Object> info(int code, String message) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", code);
        if (StringUtils.isBlank(message)) {
            result.put("message", DEFAULT_ERROR.getMessage());
        } else {
            result.put("message", message);
        }
        return result;
    }

    /**
     * @param resultStatus
     * @return
     */
    public static Map<String, Object> info(ResultStatus resultStatus) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", resultStatus.getCode());
        result.put("message", resultStatus.getMessage());
        return result;
    }

}
